package com.dropgame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.dropgame.main.AssetLoader;
import com.dropgame.main.DropGame;
import com.dropgame.tools.ImageButton2;
import com.dropgame.tools.InvertedInput;
import com.dropgame.tools.TransitionEffect;

public class FreePlayScreen implements Screen{

	/*
	 * ***********
	 * ***********
	 * NOTE THIS SCREEN IS NO LONGER USED
	 * ***********
	 * ***********
	 */
	
	private DropGame game;
	private OrthographicCamera guiCam;
	
	private static final int VIRTUAL_WIDTH = 320;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
    
    public Rectangle viewport;
    
	private SpriteBatch batcher;
	
	
	private InvertedInput input;
	private TransitionEffect transitionEffect;
	private TextureRegion gameBackGround;
	private TextureRegion button1Region;
	private ImageButton2 button1;
	private ImageButton2 button2;
	private ImageButton2 button3;
	private ImageButton2 button4;
	
	int level;
	
	public FreePlayScreen(DropGame game){
		this.game = game;
		this.guiCam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);		
		guiCam.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		
		this.viewport = new Rectangle(0.0f, 0.0f, 0.0f, 0.0f);
		batcher = new SpriteBatch();

		gameBackGround = AssetLoader.gameBackgroundRegion;
		button1Region = AssetLoader.button1;
		button1 = new ImageButton2(button1Region, 10, 250);
		button2 = new ImageButton2(AssetLoader.button2, 165, 250);
		button3 = new ImageButton2(AssetLoader.button3, 10, 85);
		button4 = new ImageButton2(AssetLoader.button4, 165, 85);
		input = new InvertedInput();
		transitionEffect = new TransitionEffect(game);
		level = 1;
	}

	@Override
	public void render(float deltaTime) {
		GL10 gl = Gdx.app.getGraphics().getGL10();
		
		guiCam.update();
		guiCam.apply(gl);
		
		gl.glViewport((int) viewport.x, (int) viewport.y,
          	          (int) viewport.width, (int) viewport.height);
		
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batcher.setProjectionMatrix(guiCam.combined);
		
		batcher.begin();
		batcher.draw(gameBackGround, 0, 0);
		batcher.draw(button1.out, button1.xPos, button1.yPos);
		batcher.draw(button2.out, button2.xPos, button2.yPos);
		batcher.draw(button3.out, button3.xPos, button3.yPos);
		batcher.draw(button4.out, button4.xPos, button4.yPos);
		batcher.end();
		transitionEffect.movingScreen(batcher, deltaTime);	
		updateLogic();
		
	}
	
	public void updateLogic(){
		if(input.justTouched()){
			if(button1.bounds.contains(input.getX(), input.getY())) {
				System.out.println("Button one was pressed");
				level = 1;
				transitionEffect.nextScreen(new GameScreen(game, level));
			}
			if(button2.bounds.contains(input.getX(), input.getY())){
				level = 2;
				transitionEffect.nextScreen(new GameScreen(game, level));
			}
			if(button3.bounds.contains(input.getX(), input.getY())){
				level = 3;
				transitionEffect.nextScreen(new GameScreen(game, level));
			}
			if(button4.bounds.contains(input.getX(), input.getY())){
				level = 4;
				transitionEffect.nextScreen(new GameScreen(game, level));
			}
		}
	}

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        //viewport = new Rectangle(crop.x, crop.y, w, h);
        viewport.set(crop.x, crop.y, w, h);
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

}
